<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工作流" prop="flowId">
        <el-select v-model="queryParams.flowId" filterable clearable placeholder="请选择工作流程">
          <el-option
            v-for="item in flowList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发起人" prop="addUserName">
        <el-input
          v-model="queryParams.addUserName"
          placeholder="请输入发起人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
     <el-table-column label="工作流" align="center" prop="flowName" />
     <el-table-column label="发起时间" align="center" prop="addTime" width="180">
       <template slot-scope="scope">
         <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
       </template>
     </el-table-column>
     <el-table-column label="发起人" align="center" prop="doc.addUserName" />
     <el-table-column label="流程状态" align="center" prop="doc.status" :formatter="flowStatusFormat"/>
     <el-table-column label="处理结果" align="center" prop="status" :formatter="statusFormat"/>
     <el-table-column label="处理时间" align="center" prop="updateTime" width="180">
       <template slot-scope="scope">
         <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
       </template>
     </el-table-column>
     <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
       <template slot-scope="scope">
         <el-button v-if="scope.row.status == '1'"
           size="mini"
           type="text"
           icon="el-icon-document-checked"
           @click="handleSignin(scope.row)"
           v-hasPermi="['oa:process:edit']"
         >签收</el-button>
         <el-button v-if="scope.row.status == '1' || scope.row.status == '2'"
           size="mini"
           type="text"
           icon="el-icon-document-copy"
           @click="handleProcess(scope.row)"
           v-hasPermi="['oa:process:edit']"
         >办理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['oa:process:list']"
          >详细</el-button>
       </template>
     </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作流步骤对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-tabs tab-position="left" style="height: 350px; overflow: auto;">
        <el-tab-pane label="办理流程">
            <el-timeline>
            <el-timeline-item v-for="(p, index) in history"
             :timestamp="p.updateTime" placement="top">
              <el-card>
                <h4>{{p.comment}}</h4>
                <p>{{p.processUser}} {{statusFormat(p)}} 于 {{p.updateTime}}</p>
              </el-card>
            </el-timeline-item>
            </el-timeline>
        </el-tab-pane>
        <el-tab-pane label="公文内容">
          <el-form ref="doc" :model="doc" label-width="80px" disabled>
          <el-form-item :label="f.field.title" v-for="f in doc.form">
            <el-input v-model="f.content" :placeholder="'请输入'+f.field.title" v-if="f.field.type == '1'"/>
            <el-date-picker clearable size="small" style="width: 200px" v-if="f.field.type == '2'"
              v-model="f.content"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              :placeholder="'选择'+f.field.title">
            </el-date-picker>
            <el-input v-model="f.content" type="textarea" :placeholder="'请输入'+f.field.title" v-if="f.field.type == '3'"/>
            <el-input-number v-model="f.content" :placeholder="'请输入'+f.field.title" v-if="f.field.type == '4'"/>
          </el-form-item>
          <el-form-item label="附件" prop="fileId" v-if="doc.file != null">
            <span v-html="getDownLink(doc)" v-if="doc.file != null"></span>
          </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="我的处理" v-if="!view">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="处理结果">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="处理意见" prop="comment">
              <el-input type="textarea" v-model="form.comment" placeholder="请输入处理意见" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { myProcess, listProcess, getProcess, signin, addProcess, updateProcess, exportProcess } from "@/api/oa/process";
import { getDocument} from "@/api/oa/document";
import { listFlow} from "@/api/oa/flow";
import { selectField} from "@/api/oa/field";

export default {
  name: "Process",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流步骤表格数据
      processList: [],
      // 工作流数据
      flowList: [],
      // 历史流程
      history:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      flowStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowId: null,
        documentId: null,
        prevId: null,
        stepId: null,
        title: null,
        status: null,
        addTime: null,
        userId: null
      },
      view:false,
      // 表单参数
      form: {},
      //公文
      doc: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "处理结果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("process_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("flow_status").then(response => {
      this.flowStatusOptions = response.data;
    });
    listFlow().then(response => {
      this.flowList = response.rows;
    });
  },
  methods: {
    /** 查询工作流步骤列表 */
    getList() {
      this.loading = true;
      myProcess(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询历史记录
    getHistory(row) {
      listProcess({documentId:row.documentId, sortName: 'pre_id', sortOrder: 'ASC'}).then(response => {
        this.history = response.rows;
        this.history.unshift({processUser:row.doc.addUserName, updateTime: row.addTime, status:'0'})
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 状态字典翻译
    flowStatusFormat(row, column) {
      return this.selectDictLabel(this.flowStatusOptions, row.doc.status);
    },
    /** 下载附件链接 */
    getDownLink(row) {
      if(row.file && row.file.id){
        return '<a href="'+process.env.VUE_APP_BASE_API+'/sys/files/download/'+row.file.id+'" target="_blank">'+row.file.title+'</a>';
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comment:null,
        flowId: null,
        documentId: null,
        prevId: null,
        stepId: null,
        title: null,
        status: "0",
        addTime: null,
        updateTime: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 办理按钮操作 */
    handleProcess(row) {
      this.reset();
      this.getHistory(row);
      getDocument(row.documentId).then(response => {
        this.doc = response.data;
      });
      this.form.id = row.id;
      this.open = true;
      this.view = false;
      this.title = "处理流程";
    },
    /** 签收按钮操作 */
    handleSignin(row) {
      const ids = row.id || this.ids;
      signin(ids).then(() => {
        this.getList();
        this.msgSuccess("签收成功");
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateProcess(this.form).then(response => {
            this.msgSuccess("处理成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      getDocument(row.documentId).then(response => {
        this.doc = response.data;
      });
      this.getHistory(row);
      this.form.id = row.id;
      this.open = true;
      this.view = true;
      this.title = "查看公文";
    },
  }
};
</script>
